#include "Config.h"



using namespace OOPUtils;



Config::Config ()
{

}


Config::~Config ()
{

}

OOPString & Config::operator [] (const OOPString & name)
{
	return _attrs[name];
}



OOPString Config::operator [] (const OOPString & name) const
{
	std::map< OOPString, OOPString >::const_iterator iter = _attrs.find (name);
	if (iter == _attrs.end ())
		return OOPString ();
	return iter->second;
}

void OOPUtils::Config::setFilePath (const OOPString & filePath)
{
	_configFilePath = filePath;
	int lastSlashIndex = filePath.find_last_of (L"/");
	int lastBackSlashIndex = filePath.find_last_of (L"\\");
	if (lastSlashIndex == OOPString::npos && lastBackSlashIndex == OOPString::npos)
		_configDirPath = L"";
	else if (lastSlashIndex == OOPString::npos)
		_configDirPath = filePath.substr (0, lastBackSlashIndex + 1);
	else if (lastBackSlashIndex == OOPString::npos)
		_configDirPath = filePath.substr (0, lastSlashIndex + 1);
	else
		_configDirPath = filePath.substr (0,
				std::max (lastSlashIndex, lastBackSlashIndex) + 1);
}

const OOPString & OOPUtils::Config::getFilePath () const
{
	return _configFilePath;
}

const OOPString & OOPUtils::Config::getDirPath () const
{
	return _configDirPath;
}

OOPUtils::OOPString OOPUtils::Config::getValAsPath (const OOPString &key) const
{
	OOPString path = (*this)[key];
	if (path.empty ())
		return path;
	if (path.find (L":") != -1)
		return path;
	return _configDirPath + path;
}
